package cn.czg.cms.netty;

import cn.czg.cms.entity.dto.MessageDTO;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

public class MessageDecoder extends ChannelInboundHandlerAdapter {

    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof TextWebSocketFrame frame) {
            String text = frame.text();
            MessageDTO messageDTO = objectMapper.readValue(text, MessageDTO.class);
            ctx.fireChannelRead(messageDTO); // ✅ 不再 release msg
        } else {
            ctx.fireChannelRead(msg);
        }
    }
}

